package com.ryskoo.basicdata.organization.repository;

import com.ryskoo.basicdata.organization.domain.OrganizationPO;
import com.ryskoo.framework.core.repository.BasicJPAImpl;
import org.hibernate.Query;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 组织机构数据处理层
 * @author thq
 */
@Repository
public class OrganizationJPAImpl extends BasicJPAImpl<OrganizationPO> implements IOrganizationRepository {
	/**
	 * 获取组织机构列表
	 * 树状结构的列表，子组织也会获取
	 * @param parentId
	 *  @return 
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<OrganizationPO> getByParentId(String parentId) {
			String hql = "select o from  OrganizationPO o where 1=1";

			// 如果没有上级机构，则查询一级机构
			if (StringUtils.isEmpty(parentId)) {
				hql += " and (o.parentId = '' or o.parentId is null)";
			} else {
				hql += " and o.parentId = :parentId";
			}
			Query query = getH4Session().createQuery(hql);

			if (!StringUtils.isEmpty(parentId)) {
				query.setString("parentId", parentId);
			}
			return query.list();
	}
}
